package cn.com.wtrj;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.StreamUtils;
import org.springframework.web.client.RestTemplate;

//创建.doc后缀的word
public class Test {

    public static void main(String[] args) throws InterruptedException {

        //writeDataDocx("E:\\tmp\\word\\学生成绩图表.doc","test", false,5);
        test2();
//        downloadFile("http://audio-list.xingfushufm.com/2018/9/第31集 (58-59章).mp3");
    }

    public static void test2(){


        for(int i = 1; i <=1130; i++){
            String url = "http://180j.ysts8.com:8000/玄幻小说/斗破苍穹-桑梓/";
            String fileName = "";

            if(i < 10){
                fileName = "00"+i;
            }
            else if(i < 100){
                fileName = "0"+i;
            }
            else {
                fileName = ""+i;
            }

            url = url + fileName + ".mp3?591909117679x1548997631x591915248339-b0067a1cd8a300b13f1d5313197b9dfb?1";

            String filePath = "E://tmp//dhdl//"+fileName+".mp3";
            System.out.println(url);
            downloadFile(url, filePath);
        }

    }

    public static void downloadFile(String strUrl, String fileName) {

//        String fileName = getFileName(src_file);

        /*int lastIndex = url.lastIndexOf("/");

        String fileName = url.substring(lastIndex+1);

        String fileNameEncodee= URLEncoder.encode(fileName);

        String urlEncodee = url.replace(fileName, fileNameEncodee);

//        System.out.println(urlEncodee);*/
//        int lastIndex = strUrl.lastIndexOf("/");
//        String fileName = "E:\\tmp\\ltr\\"+strUrl.substring(lastIndex+1);

//        try {
            try (CloseableHttpClient httpclient = HttpClients.createDefault()) {

               /* URL url = new URL(strUrl);
                URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);*/

                HttpGet httpget = new HttpGet(strUrl);
                httpget.setConfig(RequestConfig.custom() //
                        .setConnectionRequestTimeout(5 * 1000) //
                        .setConnectTimeout(600 * 1000) //
                        .setSocketTimeout(5 * 1000) //
                        .build());
                try (CloseableHttpResponse response = httpclient.execute(httpget)) {
                    org.apache.http.HttpEntity entity = response.getEntity();
                    File desc = new File(fileName);
                    File folder = desc.getParentFile();
                    folder.mkdirs();
                    try (InputStream is = entity.getContent(); //
                         OutputStream os = new FileOutputStream(desc)) {
                        StreamUtils.copy(is, os);
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
            /*} catch (URISyntaxException e) {
                e.printStackTrace();
            }*/
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}